package com.example.springboot.Redis.BaseOperation;

import com.example.springboot.MyBatis.po.t1;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 1.opsForValue
 * 1.1 添加和删除String类数据
 * 1.2 key分层级
 * 1.3 序列化对象（使用配置式的序列化工具）
 * 1.4 序列化对象（使用配置式的序列化工具）
 *
 */
@Component
@RestController
@RequestMapping("/RedisBaseOption_01")
public class Operation_01 {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    // JSON序列化工具
    private static final ObjectMapper mapper = new ObjectMapper();


    /**
     * 1.1添加和删除String类数据
     */
    @GetMapping("stringOperation_01")
    public String stringOperation_01(){
        //Redis简单示例：缓存String类型的数据
        ValueOperations valueOperations = redisTemplate.opsForValue();

        valueOperations.set("123","value");

        valueOperations.set("456","value",300, TimeUnit.SECONDS);

        //获取String类型的缓存数据
        String v = (String) valueOperations.get("123");

        //删除Redis中指定的key,通用操作直接使用redisTemplate操作即可，
        redisTemplate.delete("123");

        return v;
    }

    /**
     * 1.2 key分层级
     */
    @GetMapping("stringOperation_02")
    public String stringOperation_02(){

        ValueOperations valueOperations = redisTemplate.opsForValue();

        valueOperations.set("user:001","value",300,TimeUnit.SECONDS);

        valueOperations.set("user:002","value",300, TimeUnit.SECONDS);

        //获取String类型的缓存数据
        String v = (String) valueOperations.get("user");


        return v;

    }

    /**
     * 1.3 序列化对象（使用配置式的序列化工具）
     */
    @GetMapping("objectOperation_01")
    public String objectOperation_01(){

        ValueOperations valueOperations = redisTemplate.opsForValue();

        t1 t11 = new t1();
        t11.setId(123);
        t11.setName("hahaha");

        valueOperations.set("operationt11",t11,300,TimeUnit.SECONDS);

        System.out.println(valueOperations.get("operationt11"));

        return valueOperations.get("operationt11").toString();
    }

    /**
     * 1.4 序列化对象（使用配置式的序列化工具）
     */
    @GetMapping("objectOperation_02")
    public String objectOperation_02() throws JsonProcessingException {

        // 创建对象
        t1 user = new t1(21,"123");

        // 手动序列化
        String json = mapper.writeValueAsString(user);

        // 写入数据
        stringRedisTemplate.opsForValue().set("user:300", json,300,TimeUnit.SECONDS);

        // 获取数据
        String jsonUser = stringRedisTemplate.opsForValue().get("user:300");

        // 手动反序列化
        t1 user1 = mapper.readValue(jsonUser, t1.class);

        System.out.println("user1 = " + user1);

        return user1.toString();

    }


}
